;(function($){
	"use strict";
    var flag,
        checkbtn = $('.checkbtn');
    checkbtn.on('click',function(){
        if(!flag){
            $(this).addClass('toright');

        }else{
            $(this).removeClass('toright');
        }
        flag=!flag;                
    });

    var mask = $('.mask');


    //select city
    var storage = (function(){
        var dataDom = null,
            hostName = location.hostName ? location.hostName : "static",
            isSessionStorage = window.sessionStorage ? true : false;

        function initDataDom(){
            if(dataDom) return dataDom;

            try{
                dataDom = document.createElement('input');
                dataDom.type = 'hidden';
                dataDom.style.display = "none";
                dataDom.addBehavior('#default#userData');
                body.appendChild(this.dataDom);
                var exDate = new Date();
                exDate = exDate.getDate()+30;
                dataDom.expires = exDate.toUTCString();//设定过期时间
            } catch(e){
                dataDom = null;
            }

            return dataDom;
        }

        function setItem(key, value){
            if(isSessionStorage) {
                widnow.sessionStorage.setItem(key, value);
            } else if(initDataDom()) {
                dataDom.load(hostName);
                dataDom.setAttribute(key, value);
                dataDom.save(hostName);
            }
        }

        function getItem(key) {
            if(isSessionStorage) {
                return window.sessionStorage.getItem(key);
            } else if(initDataDom()) {
                dataDom.load(hostName);
                return dataDom.getAttribute(key);
            }
        }

        return {
            setItem: function(){
                setItem(arguments);
            },
            getItem: function(){
                getItem(arguments);
            }
        }
    })();

    function regionMap(type) {
        var oMapping = {
                "province": 1,
                "city": 2,
                "region": 3
            };

        return oMapping[type] ? oMapping[type] : 0;
    }

    // 切换元素状态
    function toggleState(o, className) {
        if(o) {
            o.addClass(className).siblings().removeClass(className);
        }
    }

    function toggleDisplay(type){
        var sNav = "",
            sItem = "";
        switch(type) {
            case 1:
                sNav = "data-type='province'";
                sItem = "data-item-type='province'";
                break;
            case 2:
                sNav = "data-type='city'";
                sItem = "data-item-type='city'";
                break;
            case 3:
                sNav = "data-type='region'";
                sItem = "data-item-type='region'";
                break;
        }

        $('[data-type="region"],[data-type="city"]').hide();

        if(sNav && sItem) {
            //显示对应的导航
            var jNav = $(".citySelect");
            toggleState(jNav.find("li[" + sNav + "]"), "current");
            //显示对应的区域
            $(".selectmain").find("ul.address_list").eq(type-1).show().siblings().hide();
        }


        for(var i=1;i<type;i++){
            $('.citySelect li').eq(i).show();
        }

    }

    // 更新导航显示区域
    function updateNavTxt(type, text) {
        var jSelector = $(".citySelect").find("li[data-type='" + type + "']");
        if(jSelector.length === 1) {
            jSelector.eq(0).html(text);
        }
    }


    $(".citySelect li").on("click", function(){
        var sType = $(this).data("type");
        // 切换"选中"状态
        toggleDisplay(regionMap(sType));
    });

    var selectTextArr = [];

    $(".selectmain li").on("click", function(e){       
        var oSelf = e.target,
            sNodeName = oSelf.nodeName.toLowerCase(),
            sText = $(oSelf).text(),
            code = $(oSelf).data('code'),
            jParent = $(oSelf).parent(),
            sType = jParent.data("item-type");

        if(sNodeName !== "li") return;

        //更新导航显示区域
        updateNavTxt(sType, '<span class="item">'+sText+'</span>');

        // 切换标签显示状态
        if(jParent.hasClass("current")) return;
        toggleState(jParent, "current");
        // 切换导航显示状态
        var nType = regionMap(sType);
        selectTextArr[nType] = sText;
        if(nType === 1) {
            updateNavTxt("city", '<span class="item">请选择</span>');
            updateNavTxt("region", "请选择");
            $(".citySelect").find("li[data-type='region']").hide();
        } else if(nType === 2) {
            updateNavTxt("region", '<span class="item">请选择</span>');
            $(".citySelect").find("li[data-type='region']").show();
        }else{
           
            mask.hide(); 
            var str = '';        
            for(var i =1,len = selectTextArr.length; i<len; i++){
                str = str + selectTextArr[i];
            }
            $('.inputArea .ipt').val(str);
        }

        nType = nType > 2 ? 0 : (nType + 1);
        toggleDisplay(nType);
        //return [sText,code];

    });    

    $('.inputArea').on('click',function(){
        mask.show();
        toggleDisplay(1);
    });












	
})(Zepto);